cmake_minimum_required(VERSION 3.16)
project(variable VERSION 1.0 LANGUAGES C CXX)

# 变量值
function(variable)
    set(myvar a b c)        #"a;b;c"
    message("${myvar}")

    set(myvar a;b;c)        #"a;b;c"
    message("${myvar}")

    set(myvar a "b c")      #"a;b c"
    message("${myvar}")

    set(foo ab)
    set(${foo} ${myvar}xy)
    message("ab = ${ab}")   #"a;b cxy"

    #避免替换
    set(shellScript [=[
#!/bin/bash
[[ -n "${USER}" ]] && "Have user"]=])
    message("${shellScript}")
endfunction()


# 获取环境变量
function(envVariable)
    message("$ENV{PATH}")
endfunction()


# 缓存变量
function(cacheVariable)
#设置缓存变量：变量值、缓存标识符、变量类型、变量注释
    set(boolVar on CACHE BOOL "bool variable")
    set(dirPathVar "/home/lhy" CACHE PATH "directory path")
    set(filePathVar "/home//lhy/a.txt" CACHE FILEPATH "file path")
    set(colorOption red CACHE STRING "color select")
    # BOOL缓存变量的简易表达式
    option(boolOption "bool option" on)
endfunction()


#列表操作
function(listOperation)
# 创建列表变量，获取列表长度
    set(mylist a b c)
    list(LENGTH mylist len)
    message("length = ${len}")

# 指定位置插入元素、尾端插入元素、首端插入元素
    list(INSERT mylist 0 d)
    list(APPEND mylist e e)
    list(PREPEND mylist f f)
    message("mylist == ${mylist}")

# 删除指定的元素、删除指定位置的元素，列表去重
    list(REMOVE_ITEM mylist f)
    list(REMOVE_AT mylist 0 1)
    list(REMOVE_DUPLICATES mylist)
    message("mylist == ${mylist}")

# 从首尾移除元素并将元素值存储到变量中
    set(mylist ${mylist} g h j)
    list(POP_FRONT mylist front1 front2)
    list(POP_BACK mylist back1 back2)
    message(("front1 == ${front1}, front2 == ${front2}, back1 == ${back1}, back2 == ${back2}"))
    message("mylist == ${mylist}")

# 获取指定位置的元素构造新的列表
    list(GET mylist 0 1 newlist)
    message("newlist == ${newlist}")

endfunction()



listOperation()

